package com.itheima.service;


import com.itheima.client.shoppingcartpojo.dto.ShoppingCartAddDto;
import com.itheima.client.shoppingcartpojo.dto.ShoppingCartdeleteDto;
import com.itheima.client.shoppingcartpojo.vo.ShoppingCartAddVo;
import com.itheima.client.shoppingcartpojo.vo.ShoppingCartListVo;
import com.itheima.client.shoppingcartpojo.vo.ShoppingCartdeleteVo;
import com.itheima.common.SnowFlake;
import com.itheima.common.TokenHolder;
import com.itheima.mapper.client.ShoppingCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author wuzhiyong
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private SnowFlake snowFlake;

    /**
     * 加入购物车
     *
     * @param shoppingCartAddDto shoppingCartAddDto
     * @return ShoppingCartAddVo
     */
    public ShoppingCartAddVo addShoppingCart(ShoppingCartAddDto shoppingCartAddDto) {

        //获取userid
        String userId = TokenHolder.getCurrentId();
        //加锁
        Boolean result = redisTemplate.opsForValue().setIfAbsent("lock" + userId, LocalDateTime.now().toString(), 100,TimeUnit.MILLISECONDS);
        if (!result){
            return null;
        }
        //获取id
        long id = snowFlake.nextId();

        //设置id
        shoppingCartAddDto.setId(String.valueOf(id));
        //设置用户id
        shoppingCartAddDto.setUserId(userId);

        //获取dishId/setmealId
        String dishId = shoppingCartAddDto.getDishId();
        String setmealId = shoppingCartAddDto.getSetmealId();

        //判断dishId/setmealId不为空
        if (dishId != null) {
            //根据dishid userid查询购物车
            ShoppingCartAddVo shoppingCartAddVoDish = shoppingCartMapper.selectShoppingCartByDishId(dishId, userId);
            //当为null时为第一次添加
            if (shoppingCartAddVoDish == null) {
                //第一次添加设置number为1
                shoppingCartAddDto.setNumber(1);
                //调用添加方法添加到数据库
                shoppingCartMapper.insertShoppingCart(shoppingCartAddDto);
            } else {
                //非第一次添加，则获取number 并+1
                shoppingCartAddDto.setNumber(shoppingCartAddVoDish.getNumber() + 1);
                Integer number = shoppingCartAddDto.getNumber();
                //调用方法更新数据库中的份数
                shoppingCartMapper.updateShoppingCartNumberByDishId(number, dishId, userId);
            }
            return shoppingCartMapper.selectShoppingCartByDishId(dishId, userId);
        } else {
            //根据setmealId userid查询购物车
            ShoppingCartAddVo shoppingCartAddVoSetmeal = shoppingCartMapper.selectShoppingCartBySetmealId(setmealId, userId);
            //当为null时为第一次添加
            if (shoppingCartAddVoSetmeal == null) {
                shoppingCartAddDto.setNumber(1);
                shoppingCartMapper.insertShoppingCart(shoppingCartAddDto);
            } else {
                shoppingCartAddDto.setNumber(shoppingCartAddVoSetmeal.getNumber() + 1);
                Integer number = shoppingCartAddDto.getNumber();
                shoppingCartMapper.updateShoppingCartNumberBySetmealId(number, setmealId, userId);
            }
            return shoppingCartMapper.selectShoppingCartBySetmealId(setmealId, userId);
        }
    }

    /**
     * 购物车列表
     *
     * @return List<ShoppingCartListVo>
     */
    public List<ShoppingCartListVo> getShoppingCart() {
        String userId = TokenHolder.getCurrentId();
        return shoppingCartMapper.selectShoppingCart(userId);

    }

    /**
     * 清空购物车
     *
     * @return 条数
     */
    public boolean removeShoppingCart() {
        String userId = TokenHolder.getCurrentId();
        return shoppingCartMapper.deleteShoppingCart(userId) > 0;
    }

    /**
     * 删除购物车商品
     *
     * @param shoppingCartdeleteDto shoppingCartdeleteDto
     * @return true false
     */
    public boolean removeShoppingCartByDishIdOrSetmenalId(ShoppingCartdeleteDto shoppingCartdeleteDto) {
        //获取 userid dishid setmealid
        String userId = TokenHolder.getCurrentId();
        String dishId = shoppingCartdeleteDto.getDishId();
        String setmealId = shoppingCartdeleteDto.getSetmealId();

        //查询购物车
        List<ShoppingCartdeleteVo> shoppingCartdeleteVos = shoppingCartMapper.selectShoppingCartByUserId(userId);
        //当购物车中无商品返回false 有则进入判断
        if (shoppingCartdeleteVos.size() != 0) {
            //根据dishid判断
            if (dishId != null) {
                //通过dishid查询购物车
                ShoppingCartAddVo shoppingCartAddVo = shoppingCartMapper.selectShoppingCartByDishId(dishId, userId);
                //获取份数
                Integer number = shoppingCartAddVo.getNumber();

                //当份数>1则份数-1，<1则删除该商品
                if (number > 1) {
                    number = number - 1;
                    return shoppingCartMapper.updateShoppingCartNumberByDishId(number, dishId, userId) > 0;
                } else {
                    return shoppingCartMapper.deleteShoppingCartByDishId(dishId) > 0;
                }
            } else {
                ShoppingCartAddVo shoppingCartAddVo = shoppingCartMapper.selectShoppingCartBySetmealId(setmealId, userId);
                Integer number = shoppingCartAddVo.getNumber();
                if (number > 1) {
                    number = number - 1;
                    return shoppingCartMapper.updateShoppingCartNumberBySetmealId(number, setmealId, userId) > 0;
                } else {
                    return shoppingCartMapper.deleteShoppingCartBySetmealId(setmealId) > 0;
                }
            }
        }
        return false;
    }
}
